/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.store.client.components;

import java.util.ArrayList;
import java.util.List;

import com.elf.gwt.common.client.model.ProductModel;
import com.elf.gwt.common.client.model.ProductReviewModel;
import com.elf.gwt.common.client.utils.CommonUtils;
import com.elf.gwt.store.client.StoreComponentController;
import com.elf.gwt.store.client.utils.StoreUtils;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTMLTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;

/**
 *  Randomly presents a Review.
 */
public class RandomReviewComponent extends ProductsComponent
{	
	protected List<ProductReviewModel> productReviewModels;

	/**
	 * Constructor
	 */
	public RandomReviewComponent(StoreComponentController controller, String stylePrefix)
	{
		super(controller, stylePrefix, "arrow_right.gif", true);
		getRightTitleImage().addClickHandler(reviewClickHandler);
		setClearManufacturerId(true);
		productReviewModels = new ArrayList<ProductReviewModel>();
	}

	protected void buildModel(JSONArray jsonArray) throws Exception
	{
		productReviewModels.clear();
		super.buildModel(jsonArray);
	}

	protected void buildModel(JSONObject jsonObject) throws Exception
	{
		ProductModel productModel = new ProductModel(jsonObject, productMetaModel.getImageWidthMax(), productMetaModel.getImageHeightMax());
		ProductReviewModel productReviewModel = new ProductReviewModel(jsonObject);
		productReviewModels.add(productReviewModel);
		productModels.add(productModel);
	}

	public void buildFromModel() throws Exception
	{
		int sz = productReviewModels.size();
		numCols = Math.min(getMaxNumCols(), sz);
		int numRows = (int)Math.round(Math.ceil((1.0 * sz)/(numCols * 1.0)));
		int colWidth = (int)Math.round(100.00 / numCols);

		bodyPanel = new Grid(numRows, numCols);
		bodyPanel.addStyleName("ComponentBodyPanel");
		bodyPanel.addStyleName(getHandle() + "BodyPanel");
		bodyPanel.addClickHandler(tableHandler); 
		CellFormatter cf = bodyPanel.getCellFormatter();

		for (int i = 0; i < sz; i++)
		{
			ProductReviewModel model = productReviewModels.get(i);
			ProductModel pmodel = productModels.get(i);

			int row = (int)Math.round(Math.floor((1.0 * i)/(numCols * 1.0)));
			int col = i - (row * numCols);

			VerticalPanel vpanel = new VerticalPanel();
			vpanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
			Image image = new Image();
			image.setUrl(StoreUtils.getImageProductsDirectory() + pmodel.getImage());
			String[] imageSize = pmodel.getImageSize();
			image.setSize(imageSize[0], imageSize[1]);
			image.setTitle(pmodel.getName());
			vpanel.add(image);

			String review = model.getReviewText();
			if (review.length() > 60) review = review.substring(0,60);
			review = CommonUtils.breakString(review, 30, '-');
			vpanel.add(new InlineLabel(review + ".."));

			Image reviewImage = new Image();
			reviewImage.setUrl(this.getImageCommonDirectory() + "stars_" + model.getRating() + ".gif");
			vpanel.add(reviewImage);

			bodyPanel.setWidget(row, col, vpanel);
			cf.addStyleName(row, col, "ComponentImage");
			cf.addStyleName(row, col, getHandle() + "Image");
			cf.addStyleName(row, col, "ComponentLink");
			cf.setHorizontalAlignment(row, col, HasHorizontalAlignment.ALIGN_CENTER);
			cf.setWidth(row, col, colWidth + "%");
		}
		panel.setWidget(1, 0, bodyPanel);
		onBuilt();
	}

	protected ClickHandler tableHandler= new ClickHandler() {
		public void onClick(ClickEvent event)
		{
			HTMLTable.Cell cell = bodyPanel.getCellForEvent(event);
			int rowIndex = cell.getRowIndex();
			int index = rowIndex;
			ProductModel pmodel = productModels.get(index);

			onSelection("ProductReviewsComponent--" + pmodel.getId() + "--" +pmodel.getCategoryId() + "--0--0--*");
		}
	};
	
	protected ClickHandler reviewClickHandler = new ClickHandler() {
		public void onClick(ClickEvent event) {
			onSelection("ProductReviewsComponent--0--0--0--0--*");
		}
	};

	public ProductReviewModel getProductReviewModel(int i) {
		return productReviewModels.get(i);
	}
}
